﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace FetionLite
{
    class FxAccountTreeNode: TreeNode
    {
        private Queue<FetionMsg> _unreadMsgQueue;

        public FetionAccount Account { get; set; }
        public DialogForm DialogForm { get; set; }
        //public Queue<FetionMsg> UnReadMsgQueue { get; set; } 

        public FxAccountTreeNode(string text)
            : base(text)
        {
            this.DialogForm = null;
            this._unreadMsgQueue = new Queue<FetionMsg>();
        }

        public int UnreadMessageCount
        {
            get { return _unreadMsgQueue.Count; }
        }

        public void EnqueueMessage(FetionMsg message)
        {
            _unreadMsgQueue.Enqueue(message);
            UpdateText();
        }

        public FetionMsg DequeueMessage()
        {
            if (_unreadMsgQueue.Count > 0)
            {
                FetionMsg msg = _unreadMsgQueue.Dequeue();
                UpdateText();
                return msg;
            }
            else
            {
                return null;
            }
        }

        private void SelfUpdateText()
        {
            if (this._unreadMsgQueue.Count == 0)
                this.Text = string.Format("{0} [{1}]", this.Account.ShowName, this.Account.OnlineStatusText);
            else
                this.Text = string.Format("{0} [{1}]({2})", this.Account.ShowName, this.Account.OnlineStatusText, this._unreadMsgQueue.Count);
        }

        private delegate void UpdateTextDelegate();
        public void UpdateText()
        {
            if (this.TreeView.InvokeRequired)
            {
                this.TreeView.BeginInvoke(new UpdateTextDelegate(SelfUpdateText));
            }
            else
                SelfUpdateText();
        }
    }

    class FxGroupTreeNode : TreeNode
    {
        public FetionGroup Group { get; set; }

        public FxGroupTreeNode(string text)
            : base(text)
        {
            this.Group = null;
        }

        private void SelfUpdateText()
        {
            int accountCount = 0;
            int onlineCount = 0;
            foreach(TreeNode node in this.Nodes)
            {
                FxAccountTreeNode accountNode = node as FxAccountTreeNode;
                if (accountNode!= null)
                {
                    accountCount++;
                    if (Fetion.Instance.IsOnlineById(accountNode.Account.Id))
                        onlineCount++;
                }
            }

            this.Text = string.Format("{0}({1}/{2})", this.Group.Name, onlineCount, accountCount);
        }

        private delegate void UpdateTextDelegate();
        public void UpdateText()
        {
            if (this.TreeView.InvokeRequired)
            {
                this.TreeView.Invoke(new UpdateTextDelegate(SelfUpdateText));
            }
            else
                SelfUpdateText();
        }
    }
}
